﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;

namespace AutonomicComputingLibrary.CHOP.CheckUP
{
    class NetworkManagement // : AutonomicComputingLibrary.ICheckUp
    {

        //multiple cards do not use singleton

        //fields

        private int AdapterIndex = 0;
        public string AdapterName { get; set; }

        public int UsagePercentage { get; set; }

        public int PercentageMin { get; set; }

        public int PercentageMax { get; set; }

        public int MaxSpeed = 0;

        public bool IsNetworkUp=false;

        public long TotalSpeed = 100;

        public NetworkManagement(string AdapterName, int Percentagemax, int Percentagemin = 50)
        {

            PercentageMin = Percentagemin;
            PercentageMax = Percentagemax;
            this.AdapterName = AdapterName;
            
            TotalSpeed =
                NetworkInterface.GetAllNetworkInterfaces().Where(b => b.Name == AdapterName).FirstOrDefault().Speed;


        }


        public int disable
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string GetAlert()
        {
            throw new NotImplementedException();
        }

        public int pause
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Health CheckUP()
        {
            try
            {
                var firstOrDefault = NetworkInterface.GetAllNetworkInterfaces().Where(b => b.Name == AdapterName).FirstOrDefault();
               
                    var ipNic = firstOrDefault.GetIPv4Statistics();

                    long val = (TotalSpeed  *  ((TotalSpeed - ipNic.BytesReceived + ipNic.BytesSent)/100));

                    return val > PercentageMax || val < PercentageMin
                               ? Health.Negative
                               : Health.Positive;
                
            }
            catch (Exception)
            {

                return Health.UnableToDetermine;
            }
        }

        public void GetHealthHistory()
        {
            throw new NotImplementedException();
        }
    }
}
